package com.example.backend.mapper;

import com.example.backend.entity.DingDan;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public interface DingDanMapper {
    @Insert("insert into dingdan(dingdan_num,user_id,shop_id,rider_id,address_id,state,money,remark,time,gratuity) values(#{dingdan_num},#{user_id},#{shop_id},#{rider_id},#{address_id},#{state},#{money},#{remark},now(),#{gratuity})")
    int addDingDan(DingDan dingDan);
    @UpdateProvider(type = DingDanMapperProvider.class, method = "getUpdateDingDanSql")
    int updateDingDan(DingDan dingDan);
    @Delete("delete from dingdan where id= #{id}")
    int deleteDingDanById(Serializable id);
    @Select("select id,dingdan_num,user_id,shop_id,rider_id,address_id,state,money,remark,time,end_time,gratuity from dingdan where id= #{id}")
    DingDan selectDingDanById(Serializable id);
    @SelectProvider(type = DingDanMapperProvider.class, method = "getSelectDingDanSql")
    ArrayList<DingDan> selectDingDans(DingDan dingdan);

    //通过订单编号查询
    @Select("select id,dingdan_num,user_id,shop_id,rider_id,address_id,state,money,remark,time,end_time,gratuity from dingdan where dingdan_num= #{dingdan_num}")
    DingDan selectDingDanByDingDanNum(String dingdan_num);
    @Select("select * from dingdan")
    List<DingDan> selectAll();

    @Select("SELECT dingdan_num FROM dingdan WHERE shop_id = #{shopId} AND state = #{state}")
    List<String> getOrderNumbersByShopAndState(Integer shopId, String state);
    
    // 添加根据日期查询订单号的方法
    @Select("SELECT dingdan_num FROM dingdan WHERE shop_id = #{shopId} AND state = #{state} AND DATE(time) = #{date}")
    List<String> getOrderNumbersByShopAndStateAndDate(Integer shopId, String state, String date);


    // 添加在接口中
    @Select("select count(*) from dingdan")
    int countOrders();

    @Select("select count(*) from dingdan where state = '5'")
    int countCompletedOrders();

    @Select("SELECT DATE_FORMAT(time, '%Y-%m') as month, COUNT(*) as count FROM dingdan WHERE state = '5' GROUP BY month ORDER BY month")
    List<Map<String, Object>> getOrderTrend();
}